package com.weighttracker.android;

import java.util.List;


import com.bappi.custom.activities.CustomActivity;
import com.bappi.ui.Chart;
import com.bappi.ui.ChartData;
import com.bappi.utils.Constants;
import com.bappi.utils.DatabaseAccessor;
import com.bappi.utils.Utils;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Tab4Activity3 extends CustomActivity {

	private Chart chart;
	private TextView title;
	private int chartType;
	private int startTimestamp;
	private int endTimestamp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_tab_4_screen_3);
			chartType = getIntent().getIntExtra(Constants.KEY_CHART_TYPE, 0);
			startTimestamp = getIntent().getIntExtra(Constants.KEY_START_DATE, 0);
			endTimestamp = getIntent().getIntExtra(Constants.KEY_END_DATE, 0);
			chart = (Chart) findViewById(R.id.chart);
			title = (TextView) findViewById(R.id.title_field);
			findViewById(R.id.button_back).setOnClickListener(
					new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							finish();
						}
					});
			
			switch (chartType) {
			case 0:
				title.setText(R.string.calories_history);
				break;
			case 1:
				title.setText(R.string.units_history);
				break;
			case 2:
				title.setText(R.string.weight_history);
				break;
			case 3:
				title.setText(R.string.weight_history);
				break;

			default:
				break;
			}

			new AsyncTask<String, String, String>() {
				private ProgressDialog progressDialog;
				private List<ChartData> chartDatas;
				private float largeCellValue;

				protected void onPreExecute() {
					try {
						progressDialog = Utils.getProgressDialog(getParent());
						progressDialog.show();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				protected String doInBackground(String... arg0) {
					try {
						switch (chartType) {
						case 0:
							chartDatas = DatabaseAccessor.getCaloriesValue(startTimestamp, endTimestamp);
							largeCellValue = 100;
							break;
						case 1:
							chartDatas = DatabaseAccessor.getUnitValue(startTimestamp, endTimestamp);
							largeCellValue = 100;
							break;
						case 2:
							chartDatas = DatabaseAccessor.getActivitiesValue(startTimestamp, endTimestamp);
							largeCellValue = 100;
							break;
						case 3:
							chartDatas = DatabaseAccessor.getWeightValue(startTimestamp, endTimestamp);
							largeCellValue = 10;
							break;

						default:
							break;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				protected void onPostExecute(String unused) {
					try {
						chart.setChartData(chartDatas,largeCellValue);
						progressDialog.dismiss();
						if(chartDatas.size()<=0){
							showAlertMessage(getParent(), getString(R.string.no_result_found));
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void showAlertMessage(Context context, String message) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message).setCancelable(false)
				.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
					public void onClick(final DialogInterface dialog,
							final int id) {
						finish();
					}
				});
		final AlertDialog alert = builder.create();
		alert.show();
	}
}
